package com.offics.loan.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offics.loan.domain.entity.FAQ;
import com.offics.loan.domain.repository.FAQRepository;
import com.offics.loan.infrastructure.persistence.mapper.FAQMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * FAQ仓储实现类
 */
@Repository
public class FAQRepositoryImpl extends ServiceImpl<FAQMapper, FAQ> implements FAQRepository {

    @Override
    public Optional<FAQ> findById(Long id) {
        return Optional.ofNullable(getById(id));
    }

    @Override
    public List<FAQ> findAllEnabled() {
        LambdaQueryWrapper<FAQ> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FAQ::getStatus, 1)
                .orderByAsc(FAQ::getSort)
                .orderByDesc(FAQ::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<FAQ> findEnabledByCategory(Integer category) {
        LambdaQueryWrapper<FAQ> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FAQ::getStatus, 1)
                .eq(FAQ::getCategory, category)
                .orderByAsc(FAQ::getSort)
                .orderByDesc(FAQ::getCreateTime);
        return list(wrapper);
    }

    @Override
    public boolean save(FAQ faq) {
        return save(faq);
    }

    @Override
    public boolean update(FAQ faq) {
        return updateById(faq);
    }

    @Override
    public boolean deleteById(Long id) {
        return removeById(id);
    }
}